// Copyright (c) 2006 Zorm
// Released under the MIT License

#include "PCH.h"
#include "Common.h"

RadarInformation::RadarInformation(tstring filename) {
	m_scale = 0.001;
	m_centerx = 0.0;
	m_centery = 0.0;
	m_panx = 0;
	m_pany = 0;
	m_focusx = 0;
	m_focusy = 0;
	parser = new NIDSParser(filename, GetRadarData());
	NIDSProductDescription productDesc = parser->GetProductDescription();
	SetLatitude(productDesc.latitude);
	SetLongitude(productDesc.longitude);
}

void RadarInformation::Draw() {
	glPushMatrix();
	glScaled(m_scale, m_scale, 1.0);
	glTranslated(m_focusx, m_focusy, 0);
	//scale size
	for(VectorPointIter iter = radarData.begin(); iter != radarData.end(); iter++) {
		PointLatLonR *pt = (*iter);
		//ATH::Instance()->GetLog()->LogEvent(Log::LOG_INFO, TEXT("(%i, %i) (%i, %i) (%i, %i) (%i, %i)\n"), pt->lat[0], pt->lon[0], pt->lat[1], pt->lon[1], pt->lat[2], pt->lon[2], pt->lat[3], pt->lon[3]);
		SetColor(pt->level);
		glBegin(GL_QUADS);
		glVertex2i(pt->lat[0], pt->lon[0]);
		glVertex2i(pt->lat[1], pt->lon[1]);
		glVertex2i(pt->lat[2], pt->lon[2]);
		glVertex2i(pt->lat[3], pt->lon[3]);
		glEnd();
		glBegin(GL_LINES);
		glVertex2i(pt->lat[0], pt->lon[0]);
		glVertex2i(pt->lat[1], pt->lon[1]);

		glVertex2i(pt->lat[1], pt->lon[1]);
		glVertex2i(pt->lat[2], pt->lon[2]);

		glVertex2i(pt->lat[2], pt->lon[2]);
		glVertex2i(pt->lat[3], pt->lon[3]);

		glVertex2i(pt->lat[3], pt->lon[3]);
		glVertex2i(pt->lat[0], pt->lon[0]);
		glEnd();
	}
	//glPopMatrix();
	//glPushMatrix();
	//no scale size
	/*GLdouble object_x = 0, object_y = 0, object_z = 0;
    
    if (gluUnProject( ATH::Instance()->GetGLContext()->GetCenterX(), ATH::Instance()->GetGLContext()->GetCenterY(), 0, model_view, projection, viewport, &object_x, &object_y, &object_z ) == TRUE) {
		object_x /= 100000000.0;
		object_x += ATH::Instance()->GetRadarInfo()->GetLatitude();
		object_y /= 100000000.0;
		object_y += ATH::Instance()->GetRadarInfo()->GetLongitude();
		ATH::Instance()->GetMainWindow()->SetLatLon(object_x, object_y);
    }*/
    glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
    int latStart = (int)floor(m_latitude) + 5;
    int lonStart = (int)floor(m_longitude) - 5;
    for (int lat = latStart; lat > latStart - 10; lat--) {
		int latP = int((lat - m_latitude) * 100000.0);
		int lonP1 = int((lonStart - m_longitude) * 100000.0);
		int lonP2 = int(((lonStart + 10) - m_longitude) * 100000.0);
		glBegin(GL_LINES);
			glVertex2i(lonP1, latP);
			glVertex2i(lonP2, latP);
		glEnd();
    }
    for (int lon = lonStart; lon <= lonStart + 10; lon++) {
		int lonP = int((lon - m_longitude) * 100000.0);
		int latP1 = int((latStart - m_latitude) * 100000.0);
		int latP2 = int(((latStart - 10) - m_latitude) * 100000.0);
		glBegin(GL_LINES);
			glVertex2i(lonP, latP1);
			glVertex2i(lonP, latP2);
		glEnd();
    }
    ATH::Instance()->GetGLContext()->LoadMatrix();
	glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
	int size = (int)ceil(3000 / (m_scale/.001));
	glBegin(GL_QUADS);
		glVertex2i(m_flatitude, m_flongitude + size);
		glVertex2i(m_flatitude + size, m_flongitude);
		glVertex2i(m_flatitude, m_flongitude - size);
		glVertex2i(m_flatitude - size, m_flongitude);
	glEnd();
	glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
	glBegin(GL_LINE_STRIP);
		glVertex2i(m_flatitude, m_flongitude + size);
		glVertex2i(m_flatitude + size, m_flongitude);
		glVertex2i(m_flatitude, m_flongitude - size);
		glVertex2i(m_flatitude - size, m_flongitude);
		glVertex2i(m_flatitude, m_flongitude + size);
	glEnd();
	ATH::Instance()->GetGLContext()->DrawString(m_flatitude, m_flongitude, m_scale, "test0");
	glPopMatrix();
}

void RadarInformation::IncreaseScale(short delta) {
	m_scale += delta * .001;
}
void RadarInformation::DecreaseScale(short delta) {
	m_scale -= delta * .001;
	if (0.001 > m_scale) {
		m_scale = 0.001;
	}
}

void RadarInformation::FocusMap(int x, int y) {
	int xpan = 0, ypan = 0;
	int zoom = (int)(1000*(GetScale() - .001));
	double mouseToWorld = (1/GetScale());
	double px = (x - ATH::Instance()->GetGLContext()->GetCenterX()) * zoom;
	double py = (y - ATH::Instance()->GetGLContext()->GetCenterY()) * zoom;

	py *= mouseToWorld;
	ypan = int(py);

	px *= mouseToWorld;
	xpan = int(px);

	SetFocusX(xpan);
	SetFocusY(ypan);
}

void RadarInformation::SetColor(unsigned char level) {
	switch (level) {
		case 0:
			glColor4ub(0, 0, 0, 255);
			return;
		case 1:
			glColor4ub(0, 236, 236, 255);
			return;
		case 2:
			glColor4ub(1, 160, 246, 255);
			return;
		case 3:
			glColor4ub(0, 0, 246, 255);
			return;
		case 4:
			glColor4ub(0, 255, 0, 255);
			return;
		case 5:
			glColor4ub(0, 200, 0, 255);
			return;
		case 6:
			glColor4ub(0, 144, 0, 255);
			return;
		case 7:
			glColor4ub(255, 255, 0, 255);
			return;
		case 8:
			glColor4ub(231, 192, 0, 255);
			return;
		case 9:
			glColor4ub(255, 144, 0, 255);
			return;
		case 10:
			glColor4ub(255, 0, 0, 255);
			return;
		case 11:
			glColor4ub(214, 0, 0, 255);
			return;
		case 12:
			glColor4ub(192, 0, 0, 255);
			return;
		case 13:
			glColor4ub(255, 0, 255, 255);
			return;
		case 14:
			glColor4ub(153, 85, 201, 255);
			return;
		case 15:
			glColor4ub(235, 235, 235, 255);
			return;
	}
}